//
//  AppManager.m
//  JSONModel
//
//  Created by ex_liuzp9 on 2025/6/4.
//

#import "AppManager.h"

NSString *const MJPropertyTypeInt = @"i";
NSString *const MJPropertyTypeShort = @"s";
NSString *const MJPropertyTypeFloat = @"f";
NSString *const MJPropertyTypeDouble = @"d";
NSString *const MJPropertyTypeLong = @"l";
NSString *const MJPropertyTypeLongLong = @"q";
NSString *const MJPropertyTypeChar = @"c";
NSString *const MJPropertyTypeBOOL1 = @"c";
NSString *const MJPropertyTypeBOOL2 = @"b";

@implementation AppManager

singleton_implementation(AppManager);

- (NSString *)doGenerateModelWithObject:(NSObject *)object {
    NSMutableString *resultString = [NSMutableString string];

    if ([object isKindOfClass:[NSDictionary class]]) {
        NSString *string = [self doGenerateModelWithDictionary:(NSDictionary *)object];
        if (string.length > 0) {
            [resultString appendFormat:@"%@", string];
        }
    } else if ([object isKindOfClass:[NSArray class]]) {
        for (NSObject *subObject in (NSArray *)object) {
            NSString *string = [self doGenerateModelWithObject:subObject];
            if (string.length > 0) {
                [resultString appendFormat:@"%@\n\n", string];
            }
        }
    }

    return resultString;
}

- (NSString *)doGenerateModelWithDictionary:(NSDictionary *)dictionary {
    NSMutableString *resultString = [NSMutableString string];

    if (dictionary.count == 0) {
        return resultString;
    }

    NSMutableArray *allKeys = [dictionary.allKeys mutableCopy];
    [allKeys sortUsingSelector:@selector(localizedStandardCompare:)];
    if ([allKeys pp_contains:^BOOL(id  _Nonnull element) {
        return ![element isKindOfClass:[NSString class]];
    }]) {
        return resultString;
    }
    for (NSString *key in allKeys) {
        NSObject *value = [dictionary valueForKey:key];
        NSString *string = [self doGeneratePropertyStringWithKey:key value:value];
        if (string.length > 0) {
            [resultString appendFormat:@"%@\n", string];
        }
    }

    return resultString;
}

- (NSString *)doGeneratePropertyStringWithKey:(NSString *)key value:(NSObject *)object {

    NSString *resultString = @"";

    if ([object isKindOfClass:[NSString class]]) {
        resultString = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;", key];
    } else if ([object isKindOfClass:[NSArray class]]) {
        resultString = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;", key];
    } else if ([object isKindOfClass:[NSDictionary class]]) {
        resultString = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;", key];
    } else if ([object isKindOfClass:[NSNumber class]]) {
        // Check the specific type of NSNumber
        const char *objCType = [((NSNumber *)object) objCType];

        if (strcmp(objCType, @encode(int)) == 0 || strcmp(objCType, @encode(short)) == 0 || strcmp(objCType, @encode(long)) == 0 || strcmp(objCType, @encode(long long)) == 0) {
            resultString = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;", key];
        } else if (strcmp(objCType, @encode(BOOL)) == 0 || strcmp(objCType, @encode(char)) == 0) {
            resultString = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;", key];
        } else if (strcmp(objCType, @encode(float)) == 0 || strcmp(objCType, @encode(double)) == 0) {
            resultString = [NSString stringWithFormat:@"@property (nonatomic, assign) CGFloat %@;", key];
        } else {
            resultString = [NSString stringWithFormat:@"@property (nonatomic, string) NSNumber *%@;", key];
        }

    } else {
        resultString = [NSString stringWithFormat:@"@property (nonatomic, strong) id %@;", key];
    }

    return resultString;
}

@end
